package com.gdsz.libbasic.utils.file

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.VectorDrawable
import android.util.DisplayMetrics
import android.view.View
import androidx.annotation.DrawableRes
import androidx.core.content.ContextCompat
import androidx.core.widget.NestedScrollView
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat

object BitmapUtil {
    /**
     * drawable 转bitmap
     * @param context Context?
     * @param drawableId Int
     * @return Bitmap?
     */
    @JvmStatic
    fun getBitmapFromDrawable(context: Context?, @DrawableRes drawableId: Int): Bitmap? {
        return when (val drawable: Drawable? = ContextCompat.getDrawable(context!!, drawableId)) {
            is BitmapDrawable -> {
                drawable.bitmap
            }
            is VectorDrawable, is VectorDrawableCompat -> {
                val bitmap: Bitmap = Bitmap.createBitmap(
                    drawable.intrinsicWidth,
                    drawable.intrinsicHeight,
                    Bitmap.Config.ARGB_8888
                )
                val canvas = Canvas(bitmap)
                drawable.setBounds(0, 0, canvas.width, canvas.height)
                drawable.draw(canvas)
                bitmap
            }
            else -> {
                throw IllegalArgumentException("unsupported drawable type")
            }
        }
    }

    /**
     * view 转bitmap
     * @param v View
     * @return Bitmap
     */
    @JvmStatic
    fun loadBitmapFromView(v: View): Bitmap {
        val w: Int = v.width
        val h: Int = v.height


        val bmp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
        val c = Canvas(bmp)

        /** 如果不设置canvas画布为白色，则生成透明  */
        // c.drawColor(Color.WHITE);
        v.layout(0, 0, w, h)
        v.draw(c)
        return bmp
    }

    /**
     * 给view进行包裹
     * @param mActivity Activity
     * @param v View
     * @param isFullScreen Boolean
     */
    // 然后View和其内部的子View都具有了实际大小，也就是完成了布局，相当与添加到了界面上。接着就可以创建位图并在上面绘制了：
    @JvmStatic
    fun layoutView(mActivity: Activity, v: View, isFullScreen: Boolean = false) {
        val metric = DisplayMetrics()
        mActivity.windowManager.defaultDisplay.getMetrics(metric)
        val width = metric.widthPixels // 屏幕宽度（像素）
        val height = metric.heightPixels // 屏幕高度（像素）
        // 整个View的大小 参数是左上角 和右下角的坐标
        v.layout(0, 0, width, height)
        val measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
        val measuredHeight = if (!isFullScreen) {
            View.MeasureSpec.makeMeasureSpec(10000, View.MeasureSpec.AT_MOST)
        } else {
            View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)
        }
        /** 当然，measure完后，并不会实际改变View的尺寸，需要调用View.layout方法去进行布局。
         * 按示例调用layout函数后，View的大小将会变成你想要设置成的大小。
         */
        v.measure(measuredWidth, measuredHeight)
        v.layout(0, 0, v.measuredWidth, v.measuredHeight)
    }


    // 然后View和其内部的子View都具有了实际大小，也就是完成了布局，相当与添加到了界面上。接着就可以创建位图并在上面绘制了：
    @JvmStatic
    fun layoutView(mActivity: Activity,view:View, width:Int,height:Int) {
        val metric = DisplayMetrics()
        mActivity.windowManager.defaultDisplay.getMetrics(metric)

        // 整个View的大小 参数是左上角 和右下角的坐标
        view.layout(0, 0, width, height)
        val measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY)
        val measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY)

        /** 当然，measure完后，并不会实际改变View的尺寸，需要调用View.layout方法去进行布局。
         * 按示例调用layout函数后，View的大小将会变成你想要设置成的大小。
         */
        view.measure(measuredWidth, measuredHeight)
        view.layout(0, 0, view.measuredWidth, view.measuredHeight)
    }
    @JvmStatic
    fun getBitmapByView(scrollView: NestedScrollView): Bitmap? {
        var h = 0
        var bitmap: Bitmap? = null
        for (i in 0 until scrollView.childCount) {
            h += scrollView.getChildAt(i).height
        }

        bitmap = Bitmap.createBitmap(scrollView.width, h, Bitmap.Config.ARGB_8888)
        val canvas = Canvas(bitmap)
        scrollView.draw(canvas)
        return bitmap
    }


}